#include "GowildResponseDeviceListCmd.h"

GowildResponseDeviceListCmd::GowildResponseDeviceListCmd(QObject *parent) :
    BaseCmd(parent)
{
    this->m_protocolType = ProtocolType::GowildProtocol;
    this->m_functionCode = FunctionCode;
}

Package GowildResponseDeviceListCmd::toPackage()
{
    Package package;

    QVariantMap jsonData;
    jsonData.insert("type","RESPONSE_DEVICE");

    QVariantList dataList;
    QList<int> deviceIdList = m_deviceHash.keys();
    for(int i=0; i<deviceIdList.size(); i++)
    {
        QVariantMap data;
        int deviceId = deviceIdList.at(i);
        Gowild::DeviceStruct device = m_deviceHash.value(deviceId);
        data.insert("id",QString::number(deviceId,10));
        data.insert("name",device.name());
        data.insert("type",device.type());
        data.insert("roomId",QString::number(device.roomId(),10));
        dataList.append(data);
    }
    jsonData.insert("data",dataList);

    QJson::Serializer serializer;
    QByteArray body = serializer.serialize(jsonData, NULL);

    package.setBody(body);
    package.setFunction(FunctionCode);

    return package;

}

void GowildResponseDeviceListCmd::unPackage(Package package)
{
    Q_UNUSED(package);
}
QHash<int, Gowild::DeviceStruct> GowildResponseDeviceListCmd::deviceHash() const
{
    return m_deviceHash;
}

void GowildResponseDeviceListCmd::setDeviceHash(const QHash<int, Gowild::DeviceStruct> &deviceHash)
{
    m_deviceHash = deviceHash;
}

